<?php
class ModelToolSaleImport extends Model {
	
	//上传订单文件
	public function uploadOrder($filename){
		echo 11111111;

		global $registry;
		$registry = $this->registry;
		set_error_handler('error_handler_for_export',E_ALL);
		register_shutdown_function('fatal_error_shutdown_handler_for_export');

		$cwd = getcwd();
		chdir( DIR_SYSTEM.'PHPExcel' );
		require_once( 'Classes/PHPExcel.php' );
		chdir( $cwd );

		$inputFileType = PHPExcel_IOFactory::identify($filename);
		$objReader = PHPExcel_IOFactory::createReader($inputFileType);
		$objReader->setReadDataOnly(true);
		$reader = $objReader->load($filename);

		$excel = $reader->getSheet(0);
		$order_product = array();
		$isFirstRow = TRUE;
		$i = 0;
		$k = $excel->getHighestRow();
		for ($i=0; $i<$k; $i+=1){

			$j = 1;
			if ($isFirstRow) {
				$isFirstRow = FALSE;
				continue;
			}

			$group = trim($this->getCell($excel,$i,$j++));
			$telephone = trim($this->getCell($excel,$i,$j++));
			$product_name = trim($this->getCell($excel,$i,$j++));

			$product_code = trim($this->getCell($excel,$i,$j++));
			if(strlen($product_code) == 10){
				$product_code = $product_code."0";
			}

			$price = trim($this->getCell($excel,$i,$j++));
			$quantity = trim($this->getCell($excel,$i,$j++));
			$total = trim($this->getCell($excel,$i,$j++));

			$time = trim($this->getCell($excel,$i,$j++));
			if($time != null){
				$dateAdded = ($time-25569)*24*60*60 - 8*60*60;
				$dateAdded = date('Y-m-d H:i', $dateAdded); 
			}
			else{
				$dateAdded = date('Y-m-d H:i');
			}
			
			//order_produt单据数组
			$order_product[] = array(
				'group' => $group,
				'telephone' => $telephone,
				'product_code' => $product_code,
				'quantity' => $quantity,
				'total' => $total,
				'date_added' => $dateAdded
			);

		}
		
		//临时数组
		$order_temp = array();
		foreach($order_product as $key => $v){
			
			$order_temp[$v['group']][] = $v;
			$order_temp[$v['group']]['order_total'] += $v['total'];
			$order_temp[$v['group']]['telephone'] = $v['telephone'];
			$order_temp[$v['group']]['date_added'] = $v['date_added'];

		}
		
		//order单据数组
		$order = array();
		foreach($order_temp as $key => $v){
			
			$order[] = array(
				'group' => $key,
				'telephone' => $v['telephone'],
				'total' => $v['order_total'],
				'date_added' => $v['date_added'],
			);

		}

		$this->createOrder($order,$order_product);

	}
	
	//生成订单相关单据
	public function createOrder($order,$order_product){
		
		
		if(count($order) > 0){
			
			//导入order表
			//$this->addOrder($order);

			//导入order_product表
			$this->addOrderProduct($order_product);

		}

	}
	
	//导入order表,同时生成order_total,order_history
	public function addOrder($order){
		
		$ok = true;
		foreach($order as $v){
			
			//获取customer信息
			$customer = $this->telToCustomer($v['telephone']);
			
			$add['store_name'] = '百货栈';
			$add['store_url'] = 'import'.date('Y-m-d');

			$add['customer_id'] = $customer['customer_id'];
			$add['customer_group_id'] = $customer['customer_group_id'];
			$add['fullname'] = $customer['fullname'];
			$add['email'] = $customer['email'];
			$add['telephone'] = $customer['telephone'];
			$add['fax'] = $customer['fax'];
			$add['custom_field'] = $customer['custom_field'];
			
			$add['payment_fullname'] = $customer['pfullname'];	
			$add['payment_company'] = $customer['pcompany'];	
			$add['payment_address'] = $customer['paddress'];				
			$add['payment_city'] = $customer['city_name'];
			$add['payment_city_id'] = $customer['city_id'];
			$add['payment_postcode'] = $customer['ppostcode'];
			$add['payment_country'] = $customer['country_name'];
			$add['payment_country_id'] = $customer['country_id'];
			$add['payment_zone'] = $customer['zone_name'];
			$add['payment_zone_id'] = $customer['zone_id'];
			$add['payment_method'] = '货到付款';
			$add['payment_code'] = 'cod';

			$add['shipping_fullname'] = $customer['pfullname'];	
			$add['shipping_company'] = $customer['pcompany'];	
			$add['shipping_address'] = $customer['paddress'];				
			$add['shipping_city'] = $customer['city_name'];
			$add['shipping_city_id'] = $customer['city_id'];
			$add['shipping_postcode'] = $customer['ppostcode'];
			$add['shipping_country'] = $customer['country_name'];
			$add['shipping_country_id'] = $customer['country_id'];
			$add['shipping_zone'] = $customer['zone_name'];
			$add['shipping_zone_id'] = $customer['zone_id'];
			$add['shipping_method'] = '免运费';
			$add['shipping_code'] = 'free.free';
			$add['shipping_telephone'] = $customer['ptelephone'];

			$add['total'] = $v['total'];
			$add['ori_total'] = $v['total'];
			$add['order_status_id'] = 5;
			$add['language_id'] = 1;
			$add['currency_id'] = 4;
			$add['currency_code'] = 'CNY';
			$add['ip'] = $customer['ip'];
			$add['user_agent'] = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36';
			$add['accept_language'] = 'zh-CN,zh;q=0.8';
			$add['date_added'] = $v['date_added'];
			$add['date_modified'] = $v['date_added'];
			$add['logcenter_id'] = $customer['logcenter_id'];
			$add['is_pay'] = 1;
			$add['recommend_usr_id'] = $customer['user_id'];

			$add['paid_time'] = $v['date_added'];
			$add['auditted_time'] = date('Y-m-d H:i',strtotime('+1 d',strtotime($v['date_added'])));
			$add['delivered_time'] = date('Y-m-d H:i',strtotime('+2 d',strtotime($v['date_added'])));
			$add['completed_time'] = date('Y-m-d H:i',strtotime('+3 d',strtotime($v['date_added'])));
			$add['out_time'] = date('Y-m-d H:i',strtotime('+2 d',strtotime($v['date_added'])));
			$add['hang_status'] = 0;
			$add['verify_status'] = 2;
			$add['first_stock_out_rate'] = 100;
			$add['final_stock_out_rate'] = 100;
			$add['total_percent'] = 100;

			$oid = M('order')->data($add)->add();
			if($oid){
				$this->addOrderTotal($oid,$v['total']); //orderTotal
				$this->addOrderHistory($oid); //orderHistory
			}
			else{
				$ok = false;
			}

		}

		return $ok;

	}
	
	//导入order_total表
	public function addOrderTotal($oid,$total){
		
		for($i=1;$i<3;$i++){

			$add['order_id'] = $oid;
			if($i == 1){
				$add['code'] = 'sub_total';
				$add['title'] = '小计';
			}
			else{
				$add['code'] = 'total';
				$add['title'] = '总计';
			}
			$add['value'] = $total;
			$add['sort_order'] = $i;

			M('order_total')->data($add)->add();
		
		}

	}
	
	//导入order_history表
	public function addOrderHistory($oid){

		$user_id = $this->user->getId();
		$username = $this->getUserName($user_id);

		$add['order_id'] = $oid;
		$add['order_status_id'] = 5;
		$add['comment'] = '后台用户: '.$username.', 上传了文件导入了此外场订单';
		$add['date_added'] = date('Y-m-d H:i');

		M('order_history')->data($add)->add();

	}
	
	//导入order_product表
	public function addOrderProduct($order_product){

		$ok = true;
		foreach($order_product as $v){
			
			print_r($v);

		}
		die();

	}
	
	//获得用户姓名
	public function getUserName($user_id){
		
		$where['user_id'] = $user_id;
		$name = M('user')->where($where)->getField('username');

		return $name;

	}
	
	//电话获取customer信息
	public function telToCustomer($tel){
		
		$where['cu.telephone'] = $tel;
		$customer = M('customer')
			->alias('cu')
			->join('address ad on ad.address_id = cu.address_id','left')
			->join('city ci on ci.city_id = ad.city_id','left')
			->join('country co on co.country_id = ad.country_id','left')
			->join('zone zo on zo.zone_id = ad.zone_id','left')
			->where($where)
			->field('cu.*,ad.fullname as pfullname,ad.company as pcompany,ad.address as paddress,ad.postcode as ppostcode,ci.name as city_name,ad.city_id as city_id,co.name as country_name,ad.country_id as country_id,zo.name as zone_name,ad.zone_id as zone_id,ad.shipping_telephone as ptelephone')
			->select();

		return $customer[0];

	}
	
	//获取excel数据
	function getCell(&$worksheet,$row,$col,$default_val=''){

		$col -= 1;
		$row += 1;
		return ($worksheet->cellExistsByColumnAndRow($col,$row)) ? $worksheet->getCellByColumnAndRow($col,$row)->getCalculatedValue() : $default_val;

	}

}
?>